package cn.glenn.demoaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class LoggerAspect {

    @Pointcut("execution(* cn.glenn.demoaop.aop.CartServiceImpl.*(..))")
    public void doTime(){}

    @Before("doTime()")
    public void beforeAddCart(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("前置通知：" + name + " 参数：" + args);
        System.out.println("开始添加购物车");
    }

    @After("doTime()")
    public void afterAddCart(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("后置通知：" + name);
        System.out.println("操作完成");
    }

    @AfterReturning(pointcut = "doTime()", returning = "returnValue")
    public void afterReturning(boolean returnValue) {
        if (returnValue) {
            System.out.println("添加购物车成功");
        } else {
            System.out.println("添加购物车失败");
        }
    }
}
//
//    @Around(value = "doSome()")
//    public void aroundAddCart(ProceedingJoinPoint joinPoint) throws Throwable {
//        try{
//            log.info("开始添加购物车");
//            boolean add = (boolean) joinPoint.proceed();
//            if (add) {
//                log.info("添加购物车成功");
//            }else log.info("添加购物车失败");
//        }catch (Throwable e){
//            log.error("添加购物车错误");
//        }finally {
//            log.info("操作完成");
//        }
//    }
//

